﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_PositionDuties : System.Web.UI.UserControl
    {
        private IPositionDutyBLL posdutyBLL = new PositionDutyBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IPositionTypeBLL postypeBLL = new PositionTypeBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IDutyBLL dutyBLL = new DutyBLL();

        private PositionDutyInfo posInfo;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            FillDataToPositionType(1);
            FillDataToDuty();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSeq.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    cmbPosType.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "PositionType.Sequence"));
                    FillDataToFieldPositions(cmbPosType.SelectedValue);                    
                    cmbFieldPos.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "FieldSeq"));
                    FillDataToPosition(Int32.Parse(cmbFieldPos.SelectedValue));
                    cmbPosition.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "PositionSeq"));
                    cmbDuty.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "DutySeq"));
                    txtDutyDescr.Text = DataBinder.Eval(DataItem, "Descr").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        public void FillDataToPositionType(int EmpType)
        {
            IList<PositionTypeInfo> Info = postypeBLL.ListPositionTypes(null, EmpType);

            cmbPosType.DataSource = Info;
        }

        public void FillDataToFieldPositions(string PosTypeSeq)
        {
            IList<FieldInfo> InfoList = fieldBLL.ListFields(PosTypeSeq, "");
  
            cmbFieldPos.DataSource = InfoList;
            cmbFieldPos.DataBind();
        }

        public void FillDataToPosition(Nullable<int> FieldSeq)
        {
            IList<PositionInfo> infoList = posBLL.ListPositions();

            cmbPosition.DataSource = infoList;
            cmbPosition.DataBind();
        }
        public void FillDataToDuty()
        {
            IList<DutyInfo> infoList = dutyBLL.ListDuties(null);

            cmbDuty.DataSource = infoList;
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            posInfo = new PositionDutyInfo();

            posInfo.PositionSeq = cmbPosition.SelectedValue.Length > 0 ? Int32.Parse(cmbPosition.SelectedValue) : (int?)null;
            posInfo.DutySeq = cmbDuty.SelectedValue.Length > 0 ? Int32.Parse(cmbDuty.SelectedValue) : (int?)null;
            posInfo.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            posInfo.CreatedBy = 1;

            posdutyBLL.Insert(posInfo);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            posInfo = new PositionDutyInfo();

            posInfo.Seq = lblSeq.Text.Length > 0 ? Int32.Parse(lblSeq.Text) : (int?)null;
            posInfo.PositionSeq = cmbPosition.SelectedValue.Length > 0 ? Int32.Parse(cmbPosition.SelectedValue) : (int?)null;
            posInfo.DutySeq = cmbDuty.SelectedValue.Length > 0 ? Int32.Parse(cmbDuty.SelectedValue) : (int?)null;
            posInfo.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            posInfo.UpdatedBy = 1;

            posdutyBLL.Update(posInfo);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbPosType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillDataToFieldPositions(cmbPosType.SelectedValue);
        }

        protected void cmbFieldPos_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillDataToPosition(cmbFieldPos.SelectedValue.Length > 0 ? Int32.Parse(cmbFieldPos.SelectedValue) : (int?)null);
        }
    }
}